package com.triones.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.triones.api.entity.*;
import com.triones.api.entity.vo.CryptoChainVO;
import com.triones.api.entity.vo.CryptoCurrencyVO;
import com.triones.api.entity.vo.HelpCategorizeVO;
import com.triones.api.entity.vo.HelpVO;
import com.triones.api.mapper.CryptoChainMapper;
import com.triones.api.mapper.CryptoCurrencyMapper;
import com.triones.api.service.CryptoChainAndCurrencyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 加密货币公链和币种 -业务实现
 */
@Service
@RequiredArgsConstructor
public class CryptoChainAndCurrencyServiceImpl implements CryptoChainAndCurrencyService {

    private final CryptoChainMapper cryptoChainMapper;

    private final CryptoCurrencyMapper cryptoCurrencyMapper;

    @Override
    public TableDataInfo<CryptoChainVO> chainPageList(CryptoChainDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<CryptoChainDO> lqw = new LambdaQueryWrapper<CryptoChainDO>()
            .eq(CryptoChainDO::getDel,"0")
            .and(StringUtils.isNotBlank(dto.getChain()),
                wq  -> wq.like(CryptoChainDO::getChain, dto.getChain()).or()
                    .like(CryptoChainDO::getAlias, dto.getChain()))
            .eq(StringUtils.isNotBlank(dto.getStatus()), CryptoChainDO::getStatus, dto.getStatus());
        Page<CryptoChainVO> page = cryptoChainMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public CryptoChainVO selectChainById(Long id) {
        return cryptoChainMapper.selectVoById(id);
    }

    @Override
    public boolean checkChainUnique(CryptoChainDO dto) {
        boolean exist = cryptoChainMapper.exists(new LambdaQueryWrapper<CryptoChainDO>()
            .eq(CryptoChainDO::getChain, dto.getChain())
            .eq(CryptoChainDO::getDel,"0")
            .ne(ObjectUtil.isNotNull(dto.getId()), CryptoChainDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public void addChain(CryptoChainDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        dto.setCreateAt(System.currentTimeMillis());
        dto.setUpdateAt(System.currentTimeMillis());
        dto.setCreateBy(loginUser.getUsername());
        dto.setUpdateBy(loginUser.getUsername());
        cryptoChainMapper.insert(dto);
    }

    @Override
    public void editChain(CryptoChainDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        dto.setUpdateAt(System.currentTimeMillis());
        dto.setUpdateBy(loginUser.getUsername());
        cryptoChainMapper.updateById(dto);
    }

    @Override
    public void removeChain(Long id) {
        CryptoChainDO cryptoChainDO = cryptoChainMapper.selectById(id);
        if (cryptoChainDO == null) {
            throw new ServiceException("数据不存在");
        }
        List<CryptoCurrencyDO> currencyDOS = cryptoCurrencyMapper.selectList(new LambdaQueryWrapper<CryptoCurrencyDO>()
            .eq(CryptoCurrencyDO::getChainId, id)
            .eq(CryptoCurrencyDO::getDel, "0"));
        if (currencyDOS.size() > 0) {
            throw new ServiceException("该公链下有币种不允许删除");
        }
        cryptoChainDO.setDel("1");
        cryptoChainMapper.updateById(cryptoChainDO);
    }

    @Override
    public List<CryptoChainVO> selectChainAll() {
        LambdaQueryWrapper<CryptoChainDO> lqw = new LambdaQueryWrapper<CryptoChainDO>();
        lqw.eq(CryptoChainDO::getDel,"0");
        return cryptoChainMapper.selectVoList(lqw);
    }

    @Override
    public TableDataInfo<CryptoCurrencyVO> currencyList(CryptoCurrencyDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<CryptoCurrencyDO> lqw = new LambdaQueryWrapper<CryptoCurrencyDO>()
            .eq(CryptoCurrencyDO::getChainId,dto.getChainId())
            .eq(CryptoCurrencyDO::getDel,"0")
            .eq(StringUtils.isNotBlank(dto.getStatus()), CryptoCurrencyDO::getStatus, dto.getStatus())
            .eq(StringUtils.isNotBlank(dto.getCcy()), CryptoCurrencyDO::getCcy, dto.getCcy());
        Page<CryptoCurrencyVO> page = cryptoCurrencyMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public CryptoCurrencyVO getCurrencyInfo(Long id) {
        return cryptoCurrencyMapper.selectVoById(id);
    }

    @Override
    public boolean checkCurrencyUnique(CryptoCurrencyDO dto) {
        boolean exist = cryptoCurrencyMapper.exists(new LambdaQueryWrapper<CryptoCurrencyDO>()
            .eq(CryptoCurrencyDO::getChainId, dto.getChainId())
            .eq(CryptoCurrencyDO::getCcy, dto.getCcy())
            .eq(CryptoCurrencyDO::getDel,"0")
            .ne(ObjectUtil.isNotNull(dto.getId()), CryptoCurrencyDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public void addCurrency(CryptoCurrencyDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        dto.setCreateAt(System.currentTimeMillis());
        dto.setUpdateAt(System.currentTimeMillis());
        dto.setCreateBy(loginUser.getUsername());
        dto.setUpdateBy(loginUser.getUsername());
        cryptoCurrencyMapper.insert(dto);
    }

    @Override
    public void editCurrency(CryptoCurrencyDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        dto.setUpdateAt(System.currentTimeMillis());
        dto.setUpdateBy(loginUser.getUsername());
        cryptoCurrencyMapper.updateById(dto);
    }

    @Override
    public void removeCurrency(Long id) {
        CryptoCurrencyDO cryptoCurrencyDO = cryptoCurrencyMapper.selectById(id);
        if (cryptoCurrencyDO == null) {
            throw new ServiceException("数据不存在");
        }
        cryptoCurrencyDO.setDel("1");
        cryptoCurrencyMapper.updateById(cryptoCurrencyDO);
    }
}
